home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 458 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  67 lines

  1. Newsgroups: comp.std.c
  2. Path: blackbush.xlink.net!slsv6bt!news
  3. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  4. Subject: Copying structures
  5. Message-ID: <KANZE.96Feb28105936@slsvewt.lts.sel.alcatel.de>
  6. Sender: news@lts.sel.alcatel.de
  7. Organization: GABI Software, Sarl.
  8. Date: 28 Feb 1996 09:59:35 GMT
  9.  
  10. I seem to recall hearing that there was a technical correction or a
  11. clarification concerning this.  Could anyone confirm this.
  12.  
  13. Is the following program well defined:
  14.  
  15.     #include <string.h>
  16.  
  17.     struct S { char s[ 20 ] ; } ;
  18.  
  19.     struct S
  20.     f()
  21.     {
  22.         struct S        s ;
  23.         strcpy( s.s , "abc" ) ;
  24.         return s ;
  25.     }
  26.  
  27. I am particularly concerned about the fact that copying the structure
  28. out as a return value involves copying (reading) uninitialized data.
  29.  
  30. On a similar vein, using the same struct:
  31.  
  32.     void
  33.     f( FILE* fp )
  34.     {
  35.         struct S        s ;
  36.         strcpy( s.s , "abc" ) ;
  37.         fwrite( &s , sizeof( struct S ) , 1 , fp ) ;
  38.     }
  39.  
  40. Is this guaranteed not to invoke undefined behavior?  (It is
  41. interesting to note that Purify detects an error -- uninitialized
  42. memory read -- in this case.)
  43.  
  44. (Note that in both cases, I don't really care what the compiler does
  45. with the uninitialized values.  It can change them in any way it
  46. likes.)
  47.  
  48. Note that while I can hardly imagine an implementation where this
  49. fails, if the struct is changed to something like:
  50.  
  51.     struct MultiDimPoint
  52.     {
  53.         int             numDims ;
  54.         double          dims[ MAXDIMS ] ;
  55.     } ;
  56.  
  57. It is not hard to conceive of implementations where this would fail.
  58. (The compiler generates a loop using floating point loads and stores
  59. to copy the doubles, the machine uses IEEE format, and the bit pattern
  60. in one of the uninitialized doubles is a signaling NaN.)
  61. -- 
  62. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  63. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  64. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  65.                 -- A la recherche d'une activitΘ dans une region francophone
  66.  
  67.